package com.song.guli.service.edu.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.song.guli.common.base.result.R;
import com.song.guli.common.base.result.ResultCodeEnum;
import com.song.guli.common.base.util.ExceptionUtils;
import com.song.guli.common.base.util.JwtInfo;
import com.song.guli.common.base.util.JwtUtils;
import com.song.guli.service.base.dto.MemberDto;
import com.song.guli.service.base.exception.GuliException;
import com.song.guli.service.edu.entity.Comment;
import com.song.guli.service.edu.fegin.UcenterFeginService;
import com.song.guli.service.edu.service.CommentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Song
 * @since 2021-03-26
 */
@RestController
@RequestMapping("/api/edu/comment")
public class ApiCommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UcenterFeginService ucenterFeginService;

    //根据课程id_分页查询课程评论的方法
    @GetMapping("/getCommentPage/{page}/{limit}")
    public R getCommentPage(@PathVariable Long page,@PathVariable Long limit,String courseId){
        Page<Comment> commentPage = new Page<>(page, limit);
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        //判断课程id是否为空
        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        //按最新排序
        wrapper.orderByDesc("gmt_create");

        //数据会被封装到commentPage中
        commentService.page(commentPage,wrapper);

        List<Comment> commentList = commentPage.getRecords();
        long current = commentPage.getCurrent();//当前页
        long size = commentPage.getSize();//一页记录数
        long total = commentPage.getTotal();//总记录数
        long pages = commentPage.getPages();//总页数
        boolean hasPrevious = commentPage.hasPrevious();//是否有上页
        boolean hasNext = commentPage.hasNext();//是否有下页

        HashMap<String, Object> map = new HashMap<>();
        map.put("current",current);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("list",commentList);
        return R.ok().data(map);
    }

    //添加评论
    @PostMapping("/auth/addComment")
    public R addComment(HttpServletRequest request, @RequestBody Comment comment){
        JwtInfo jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (jwtToken==null){
            throw new GuliException(ResultCodeEnum.LOGIN_AUTH.getCode(),ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        comment.setMemberId(jwtToken.getId());
        //远程调用ucenter根据用户id获取用户信息
        MemberDto memberVo = ucenterFeginService.getMemberByMemberId(jwtToken.getId());

        comment.setAvatar(memberVo.getAvatar());
        comment.setNickname(memberVo.getNickname());
        comment.setGmtCreate(new Date());
        //保存评论
        commentService.save(comment);
        return R.ok().data("success",true);
    }

}

